package com.coolgor.coldot.dao.wechat.qyh;

import java.util.List;

import com.coolgor.coldot.entity.wechat.qyh.QYHAccessToken;
import org.apache.ibatis.annotations.*;


@Mapper
public interface QYHAccessTokenDao {

	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_wechat_qyh_accesstoken
			WHERE
				record_id = #{recordId}	
		</script>		
	""")
	@Results(id = "QYHAccessTokenMap", value = {
			@Result(property = "recordId", column = "record_id"),
			@Result(property = "corpId", column = "corp_id"),
			@Result(property = "corpName", column = "corp_name"),
			@Result(property = "agentId", column = "agent_id"),
			@Result(property = "agentName", column = "agent_name"),
			@Result(property = "secret", column = "secret"),
			@Result(property = "accessToken", column = "access_token"),
			@Result(property = "createTime", column = "create_time"),
			@Result(property = "invalidTime", column = "invalid_time"),
			@Result(property = "expireTime", column = "expire_time")
	})
	QYHAccessToken queryQYHAccessTokenById(long recordId);


	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_wechat_qyh_accesstoken
			WHERE
				<if test="qyhAccessTokenCondition.recordId != null">
					and record_id =	#{qyhAccessTokenCondition.recordId}
				</if>
				<if test="qyhAccessTokenCondition.corpId != null">
					and corp_id = #{qyhAccessTokenCondition.corpId}
				</if>
				<if test="qyhAccessTokenCondition.corpName != null">
					and corp_name like '%${qyhAccessTokenCondition.corpName}%'
				</if>
				<if test="qyhAccessTokenCondition.agentId != null">
					and agent_id = #{qyhAccessTokenCondition.agentId}
				</if>
				<if test="qyhAccessTokenCondition.agentName != null">
					and agent_name like '%${qyhAccessTokenCondition.agentName}%'
				</if>	
		</script>		
	""")
	@ResultMap("QYHAccessTokenMap")
	List<QYHAccessToken> queryQYHAccessTokenList(@Param("qyhAccessTokenCondition") QYHAccessToken qyhAccessTokenCondition);


	@Insert("""
		<script>
		INSERT INTO tb_coldot_wechat_qyh_accesstoken
			(corp_id,corp_name,agent_id,agent_name,secret,
			access_token,create_time,invalid_time,expire_time)
		VALUES
			(#{corpId},#{corpName},#{agentId},#{agentName},#{secret},
			#{accessToken},#{createTime},#{invalidTime},#{expireTime})	
		</script>		
	""")
	@Options(useGeneratedKeys = true, keyColumn = "record_id", keyProperty = "recordId")
	int insertQYHAccessToken(QYHAccessToken qyhAccessToken);


	@Update("""
		<script>
			update tb_coldot_wechat_qyh_accesstoken
			<set>
				<if test="corpId != null">corp_id=#{corpId},</if>
				<if test="corpName != null">corp_name=#{corpName},</if>
				<if test="agentId != null">agent_id=#{agentId},</if>
				<if test="agentName != null">agent_name=#{agentName},</if>
				<if test="secret != null">secret=#{secret},</if>
				<if test="accessToken != null">access_token=#{accessToken},</if>
				<if test="createTime != null">create_time=#{createTime},</if>
				<if test="invalidTime != null">invalid_time=#{invalidTime},</if>
				<if test="expireTime != null">expire_time=#{expireTime}</if>
			</set>
			where record_id=#{recordId}	
		</script>		
	""")
	int updateQYHAccessToken(QYHAccessToken qyhAccessToken);


	@Delete("""
		<script>
			DELETE FROM
				tb_coldot_wechat_qyh_accesstoken
			WHERE
				record_id=#{recordId}	
		</script>		
	""")
	int deleteQYHAccessToken(QYHAccessToken qyhAccessToken);


	@Delete("""
		<script>
			DELETE FROM
				tb_coldot_wechat_qyh_accesstoken
			WHERE record_id IN
			<foreach collection="list" item="recordId" open="(" separator="," close=")">
				#{recordId}
			</foreach>	
		</script>		
	""")
	int batchDeleteQYHAccessToken(List<Long> recordIdList);

}
